package com.zp.business.controller.template;

import com.zp.business.entity.template.bo.TemplateDetailBo;
import com.zp.business.entity.template.vo.TemplateDetailVo;
import com.zp.business.service.template.ITemplateDetailService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 模板子表控制器
 * 前端访问路由地址为:/business/templateDetail
 *
 * @author zhang peng
 * @ date 2024-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/templateDetail")
public class TemplateDetailController extends BaseController {

    private final ITemplateDetailService iTemplateDetailService;

    /**
     * 查询模板子表列表 (分页)
     */
    @GetMapping("/selectPage")
    public TableDataInfo<TemplateDetailVo> selectPage (TemplateDetailBo bo, PageQuery pageQuery) {
        return iTemplateDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询模板子表列表 (不分页)
     */
    @GetMapping("/list")
    public R<List<TemplateDetailVo>> list (TemplateDetailBo bo) {
        List<TemplateDetailVo> list = iTemplateDetailService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出模板子表列表
     */
    @Log(title = "模板子表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (TemplateDetailBo bo, HttpServletResponse response) {
        List<TemplateDetailVo> list = iTemplateDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "模板子表", TemplateDetailVo.class, response);
    }

    /**
     * 获取模板子表详细信息
     *
     * @param detailId 主键
     */
    @GetMapping("/{detailId}")
    public R<TemplateDetailVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String detailId) {
        return R.ok(iTemplateDetailService.queryById(detailId));
    }

    /**
     * 新增模板子表
     */
    @Log(title = "模板子表", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody TemplateDetailBo bo) {
        return toAjax(iTemplateDetailService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改模板子表
     */
    @Log(title = "模板子表", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody TemplateDetailBo bo) {
        return toAjax(iTemplateDetailService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除模板子表
     *
     * @param detailIds 主键串
     */
    @Log(title = "模板子表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] detailIds) {
        return toAjax(iTemplateDetailService.deleteWithValidByIds(Arrays.asList(detailIds), true) ? 1 : 0);
    }
}
